import * as CONSTANTS from "@/config/constants.js";

export function isMobile() {
    return /android|blackberry|iemobile|ipad|iphone|ipod|opera mini|webos/i.test(navigator.userAgent);
}

export function isWx() {
    return /miniProgram/i.test(navigator.userAgent);
}
export function isIos() {
    const ua = navigator.userAgent.toLowerCase();
    return /iphone|ipad|ipod/.test(ua);
}
export function isAndroid() {
    return /android/i.test(navigator.userAgent);
}

export function isSameDay(date1, date2) {
    return (
        date1.getFullYear() === date2.getFullYear() &&
        date1.getMonth() === date2.getMonth() &&
        date1.getDate() === date2.getDate()
    );
}

/**
 * 将日期格式化成指定格式的字符串
 * @param date 要格式化的日期，不传时默认当前时间，也可以是一个时间戳
 * @param fmt 目标字符串格式，支持的字符有：y,M,d,q,w,H,h,m,S，默认：yyyy-MM-dd HH:mm:ss
 * @returns 返回格式化后的日期字符串
 */
export function formatDate(date, fmt) {
    date = date == undefined ? new Date() : date
    date = typeof date === 'number' ? new Date(date) : date
    fmt = fmt || 'yyyy-MM-dd HH:mm:ss'
    var obj =
    {
        'y': date.getFullYear(), // 年份，注意必须用getFullYear
        'M': date.getMonth() + 1, // 月份，注意是从0-11
        'd': date.getDate(), // 日期
        'q': Math.floor((date.getMonth() + 3) / 3), // 季度
        'w': date.getDay(), // 星期，注意是0-6
        'H': date.getHours(), // 24小时制
        'h': date.getHours() % 12 == 0 ? 12 : date.getHours() % 12, // 12小时制
        'm': date.getMinutes(), // 分钟
        's': date.getSeconds(), // 秒
        'S': date.getMilliseconds() // 毫秒
    }
    var week = ['天', '一', '二', '三', '四', '五', '六']
    for (var i in obj) {
        fmt = fmt.replace(new RegExp(i + '+', 'g'), function (m) {
            var val = obj[i] + ''
            if (i == 'w') return (m.length > 2 ? '星期' : '周') + week[val]
            for (var j = 0, len = val.length; j < m.length - len; j++) val = '0' + val
            return m.length == 1 ? val : val.substring(val.length - m.length)
        })
    }
    return fmt
}

//海况弹窗经纬度format
export function formatDisplayLatLng(wrapLatLng) {
    const lat = Math.abs(wrapLatLng.lat);
    const lng = Math.abs(wrapLatLng.lng);
    const latDeg = Math.trunc(lat);
    const latMin = Math.trunc((lat - latDeg) * 60);
    const latSec = (((lat - latDeg) * 60 - latMin) * 60).toFixed(0);
    const lngDeg = Math.trunc(lng);
    const lngMin = Math.trunc((lng - lngDeg) * 60);
    const lngSec = (((lng - lngDeg) * 60 - lngMin) * 60).toFixed(0);
    let displayLng = lngDeg + '°' + lngMin + '\'' + lngSec + '\"';
    displayLng += wrapLatLng.lng >= 0 ? "E" : "W"
    let displayLat = latDeg + '°' + latMin + '\'' + latSec + '\"';
    displayLat += wrapLatLng.lat >= 0 ? "N" : "S"
    return {
        lat: displayLat,
        lng: displayLng
    };
}

export function formatDisplayData(feature, value) {
    if (!feature || !value) {
        return { value: '', unit: '' }
    }
    let unit = feature.unit;
    if (feature.name === CONSTANTS.OceanFeatureEnum.WIND.name) {
        //风速转换成风级
        value = Number.isNaN(Number(value)) ? 0 : Number(value);
        value = convertWindSpeedToLevel(value);
        unit = '级';
    } else {
        value = Number.isNaN(Number(value)) ? ' ' : Number(value);
    }
    if (feature.name === CONSTANTS.OceanFeatureEnum.CURRENT.name) {
        //洋流单位由m/s转换为nmi/h
        value = (Number(value) * 1.94384).toFixed(3);
        unit = 'kt'
    }
    if (feature.name === CONSTANTS.OceanFeatureEnum.MLSP.name) {
        value = (Number(value) / 1000).toFixed(2);
        unit = 'kPa'
    }
    if (feature.name === CONSTANTS.OceanFeatureEnum.TEMP.name || feature.dataName === CONSTANTS.OceanFeatureEnum.HI_SST.name ||
        feature.name === CONSTANTS.OceanFeatureEnum.WAVE_HEIGHT.name || feature.dataName === CONSTANTS.OceanFeatureEnum.SALT.name) {
        value = value.toFixed(1);
    }
    // if (VueStore.state.depth.compareResult && VueStore.state.depth.depth !== 0) {
    //     featureName = feature.dataName === CONSTANTS.OCEAN_FEATURE_NAME.CURRENT ? '水下洋流' : featureName;
    //     featureName = feature.dataName === CONSTANTS.OCEAN_FEATURE_NAME.TEMP ? '水下温度' : featureName;
    // }
    return { value, unit };
}

/**
* 风速和风级转换
*/
function convertWindSpeedToLevel(windSpeed) {
    let windLevel;
    switch (true) {
        case windSpeed <= 0.2:
            windLevel = 0;
            break;
        case windSpeed <= 1.5:
            windLevel = 1;
            break;
        case windSpeed <= 3.3:
            windLevel = 2;
            break;
        case windSpeed <= 5.4:
            windLevel = 3;
            break;
        case windSpeed <= 7.9:
            windLevel = 4;
            break;
        case windSpeed <= 10.7:
            windLevel = 5;
            break;
        case windSpeed <= 13.8:
            windLevel = 6;
            break;
        case windSpeed <= 17.1:
            windLevel = 7;
            break;
        case windSpeed <= 20.7:
            windLevel = 8;
            break;
        case windSpeed <= 24.4:
            windLevel = 9;
            break;
        case windSpeed <= 28.4:
            windLevel = 10;
            break;
        case windSpeed <= 32.6:
            windLevel = 11;
            break;
        case windSpeed <= 36.9:
            windLevel = 12;
            break;
        case windSpeed <= 41.4:
            windLevel = 13;
            break;
        case windSpeed <= 46.1:
            windLevel = 14;
            break;
        case windSpeed <= 50.9:
            windLevel = 15;
            break;
        case windSpeed <= 56:
            windLevel = 16;
            break;
        case windSpeed <= 61.2:
            windLevel = 17;
            break;
    }
    return windLevel
}


export function convertOceanFeatureData(oceanFeature, data) {
    let featureValue;
    if (oceanFeature.name === CONSTANTS.OceanFeatureEnum.WIND.name) {
        featureValue = Number.isNaN(Number(data))
            ? 0
            : Number(data);
        featureValue = convertWindSpeedToLevel(featureValue);
        return featureValue + "  级";
    } else if (oceanFeature.name === CONSTANTS.OceanFeatureEnum.CURRENT.name) {
        featureValue = Number.isNaN(Number(data))
            ? 0
            : (Number(data) * 1.94384).toFixed(3);
        return featureValue + " kt";
    } else if (oceanFeature.name === CONSTANTS.OceanFeatureEnum.MLSP.name) {
        featureValue = Number.isNaN(Number(data))
            ? 0
            : (Number(data) / 1000).toFixed(2);
        return featureValue + " kPa";
    } else {
        featureValue = Number.isNaN(Number(data))
            ? " "
            : Number(data);
    }
    let unit = oceanFeature.unit;
    return featureValue + " " + unit;
}

export function getRectangleGeometryCenter(geometry) {
    const lng = (geometry.coordinates[0][0][0] + geometry.coordinates[0][1][0] + geometry.coordinates[0][2][0] + geometry.coordinates[0][3][0]) / 4;
    const lat = (geometry.coordinates[0][0][1] + geometry.coordinates[0][1][1] + geometry.coordinates[0][2][1] + geometry.coordinates[0][3][1]) / 4;
    return [lat , lng];
}